package cheme.ProcessModel.Cmds;

import java.util.ArrayList;
import java.util.List;

import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IParent;

public class ReplaceCmd extends ChemeCommand
{
	
	List<IChild> add;
	
	IChild remove;
	int removeIndex;
	
	IParent parent;
	
	public ReplaceCmd(IChild remove, IChild add)
	{
		super(remove, "Replace command");
		
		setRemove(remove);
		setAdd(add);
	}
	
	public ReplaceCmd(IChild remove, List<IChild> add)
	{
		super(remove, "Replace command");
		
		setRemove(remove);
		setAdd(add);
	}
	
	private void setAdd(List<IChild> add)
	{
		this.add = add;
	}
	
	private void setAdd(IChild add)
	{
		this.add = new ArrayList<IChild>();
		this.add.add(add);
	}
	
	private void setRemove(IChild remove)
	{
		this.remove = remove;
	}

	@Override
	protected void primExecute()
	{
		parent = remove.getParent();
		int insertIndex = removeIndex = parent.indexOf(remove);
		
		//TODO: ensure this does not wreak havoc on other parts of the system
		//Specifically the ordering of remove, then add.

		parent.replace(remove, add);
	}

	@Override
	protected void primRedo()
	{
		parent.replace(remove, add);
	}
	
	@Override
	protected void primUndo()
	{
		parent.removeAll(add);
		parent.add(remove, removeIndex);
	}
}